<?php

namespace api\modules\app\traits;

use api\models\AreaDict;
use api\modules\app\models\MetaArea;
use Yii;
use yii\db\Query;
use yii\helpers\ArrayHelper;

trait TranslateTrait
{
    public $cacheKey = 'paidan_translateData';
    public $data = [];
    public $area = [];
    public $gridCodes = [];

    public function translateCode($code, $type = 'area', &$transData = [])
    {
        if (empty($code)) {
            return '';
        }
        if ($type == 'area') {
            if (empty($transData['area'])) {
                $transData['area'] = $this->getAreaData();
            }
            return $transData['area'][$code] ?? '';
        }
        if ($type == 'department') {
            $department = MetaArea::findOne(['code' => $code]);
            return $department->name ?? '';
        }
        if ($type == 'gridCenter') {
            return $code;
            $data = $this->getGridCodeData();
            return $data[$code] ?? '';
        }
        return '';
    }

    private function getAreaData()
    {
        $cacheKey = 'paidan_translate_area';
        if ($this->area) {
            return $this->area;
        }
        $data = Yii::$app->redisCache->get($cacheKey);
        if ($data) {
            $data = json_decode($data, true);
            $this->area = $data;
            return $data;
        }

        $data = [];
        $query = AreaDict::find();
        foreach ($query->each() as $item) {
            $data[$item['code']] = $item['name'];
        }
        $this->area = $data;
        Yii::$app->redisCache->set($this->cacheKey, json_encode($data), 3600 * 24);
        return $data;
    }

    private function getGridCodeData()
    {
        $cacheKey = 'paidan_translate_grid_code';
        if ($this->gridCodes) {
            return $this->gridCodes;
        }
        $data = Yii::$app->redisCache->get($cacheKey);
        if ($data) {
            $data = json_decode($data, true);
            $this->gridCodes = $data;
            return $data;
        }

        $data = [];
        $query = MetaArea::find()->where(['category' => '网格']);
        foreach ($query->each() as $item) {
            $data[$item['code']] = $item['name'];
        }
        $this->gridCodes = $data;
        Yii::$app->redisCache->set($this->cacheKey, json_encode($data), 3600 * 24);
        return $data;
    }

    public function translate($key, $type, $defaultValue = null, $defaultDictData = [])
    {
        $defaultValue = $defaultValue === null ? $key : $defaultValue;
        $dictData = !empty($defaultDictData) ? $defaultDictData : $this->getDictData();
        return ArrayHelper::getValue($dictData, [$type, $key, 'display_name'], $defaultValue);
    }

    public function getDictData()
    {
        if ($this->data) {
            return $this->data;
        }
        $data = Yii::$app->redisCache->get($this->cacheKey);
        if ($data) {
            $data = json_decode($data, true);
        } else {
            // 暂时使用 test2
            $db = Yii::$app->test2;
            $data = (new Query())->from('meta_dict_data')->all($db);
            $data = ArrayHelper::index($data, 'name', 'type');

            //获取街道   区域  网格
            $areaData = (new Query())->from('meta_area')->all($db);
            foreach ($areaData as $area){
                if ($area['category'] === '区') {
                    $data['district'][$area['id']] = [
                        "name" => $area['id'],
                        "display_name" => $area['name'],
                    ];
                } else if ($area['category'] === '街道') {
                    $data['street'][$area['id']] = [
                        "name" => $area['id'],
                        "display_name" => $area['name'],
                    ];
                } else if ($area['category'] === '重点区域') {
                    $data['keyDistrict'][$area['id']] = [
                        "name" => $area['id'],
                        "display_name" => $area['name'],
                    ];
                } else if ($area['category'] === '网格') {
                    $data['gridCenter'][$area['id']] = [
                        "name" => $area['id'],
                        "display_name" => $area['name'],
                        "code" => $area['code']
                    ];
                } else if ($area['category'] === '区部门' || $area['category'] === '街道部门' || $area['category'] === '市部门') {
                    $data['department'][$area['id']] = [
                        "name" => $area['id'],
                        "display_name" => $area['name']
                    ];
                }
            }
            Yii::$app->redisCache->set($this->cacheKey, json_encode($data));
        }
        $this->data = $data;

        return $data;
    }
}
